﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OPMS_V1.BOL;
using System.Data;

namespace OPMS_V1
{
    public partial class Home1 : System.Web.UI.Page
    {
        ChooseState objChooseState = new ChooseState();
        //CheckBoxList chklist1 = new CheckBoxList();

        DataSet ds = new DataSet();
        GetAreafromDistrict objArea = new GetAreafromDistrict();

        /// <summary>
        /// This function is used for the Page load events.
        /// It executes every time when the Event Occurs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            string mode = null;
            string selectedState = null;
            string selectedDistricts = null;

            try
            {
                mode = Request["Mode"].ToString();
                selectedState = Request["State"];
                selectedDistricts = Request["district"];
            }
            catch (Exception)
            {
                Response.Redirect("~\\ErrorHandling\\ErrorPage.aspx");
            }

            //Session["statusPage"] = mode;

            #region Setting of Appereance of a page based on their Modes
            if (Session["statusPage"].Equals("buy"))
            {
                lblSearch.ForeColor = System.Drawing.Color.IndianRed;
                lblSearch.Text = "Search property to Buy";
                btnSearch.BackColor = System.Drawing.Color.IndianRed;
                lblImageMessage.ForeColor = System.Drawing.Color.IndianRed;
                lblSearchHeading.ForeColor = System.Drawing.Color.IndianRed;
                //btnSearchProperty.BackColor = System.Drawing.Color.IndianRed;
                lblSelectAreas.ForeColor = System.Drawing.Color.IndianRed;
                btnGetArea.BackColor = System.Drawing.Color.IndianRed;
            }
            else if (Session["statusPage"].Equals("rent"))
            {
                lblSearch.ForeColor = System.Drawing.Color.Purple;
                lblSearch.Text = "Search property to Rent";
                btnSearch.BackColor = System.Drawing.Color.Purple;
                lblImageMessage.ForeColor = System.Drawing.Color.Purple;
                lblSearchHeading.ForeColor = System.Drawing.Color.Purple;
                //btnSearchProperty.BackColor = System.Drawing.Color.Purple;
                lblSelectAreas.ForeColor = System.Drawing.Color.Purple;
                btnGetArea.BackColor = System.Drawing.Color.Purple;
            }
            else if (Session["statusPage"].Equals("share"))
            {
                lblSearch.ForeColor = System.Drawing.Color.SkyBlue;
                lblSearch.Text = "Search property to Share";
                btnSearch.BackColor = System.Drawing.Color.SkyBlue;
                lblImageMessage.ForeColor = System.Drawing.Color.SkyBlue;
                lblSearchHeading.ForeColor = System.Drawing.Color.SkyBlue;
                //btnSearchProperty.BackColor = System.Drawing.Color.SkyBlue;
                lblSelectAreas.ForeColor = System.Drawing.Color.SkyBlue;
                btnGetArea.BackColor = System.Drawing.Color.SkyBlue;
            }
            #endregion Setting of Appereance of a page based on their Modes

            #region display settings on the basis of selected states and District
            if (selectedState == null && selectedDistricts == null)
            {
                pnlBuy.Visible = false;
                pnlDistrictList.Visible = false;
                BuySearch.Visible = true;
               
                pnlDistrictList.Visible = false;
                lstAreaList.Visible = false;
                lblSearchHeading.Visible = false;
                lblSelectAreas.Visible = false;
            }
            else if (selectedState != null && selectedDistricts == null)
            {
                lblSearchHeading.Text = "To search in " + selectedState + ", select the districts";
                pnlDistrictList.Visible = true;
                pnlBuy.Visible = true;
                BuySearch.Visible = false;
                lstAreaList.Visible = false;
                lblSearchHeading.Visible = true;
                lblSelectAreas.Visible = false;
            #endregion display settings on the basis of selected states and District

                List<String> districts = new List<string>();
                districts = objChooseState.getDistrict(selectedState);

                switch (selectedState)
                {
                    case "Jammu Kashmir":
                        MakeCheckBox(districts);
                        break;
                    case "Bihar":
                        MakeCheckBox(districts);
                        break;
                    case "Jharkhand":
                        MakeCheckBox(districts);
                        break;
                    case "Himachal Pradesh":
                        MakeCheckBox(districts);
                        break;
                    case "Uttar Pradesh":
                        MakeCheckBox(districts);
                        break;
                    case "Delhi":
                        MakeCheckBox(districts);
                        break;
                    case "Haryana":
                        MakeCheckBox(districts);
                        break;
                    case "Punjab":
                        MakeCheckBox(districts);
                        break;
                    case "Chandigarh":
                        MakeCheckBox(districts);
                        break;
                }
            }
            else if (selectedState != null && selectedDistricts != null)
            {
                lblSearchHeading.Visible = false;
                pnlDistrictList.Visible = false;
                pnlBuy.Visible = true;
                BuySearch.Visible = false;
                lstAreaList.Visible = true;
                lblSelectAreas.Visible = true;

                string[] tempArray;

                try
                {
                    tempArray = selectedDistricts.Split(',');
                }
                catch (Exception)
                {
                    //lblError.Text = "Please select atleast one district."+ex.Message;
                }

                ds = objArea.retrieveDistrictArea(selectedDistricts);
                List<string> AreaList = new List<string>();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string test = dr["area"].ToString();
                    AreaList.Add(dr["area"].ToString());
                }
                
                //lstAreaList.DataSource = ds;
                //lstAreaList.DataTextField = ds.Tables[0].Columns["area"].ColumnName.ToString();
                //lstAreaList.DataValueField = ds.Tables[0].Columns["area"].ColumnName.ToString();
                //lstAreaList.DataBind();
                MakeAreaCheckBox(AreaList);
                //lstAreaList.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// This function is used for Making the CheckBoxes for the Districts
        /// </summary>
        /// <param name="districtname"></param>
        public void MakeCheckBox(List<string> districtname)
        {
            int count = districtname.Count;

            foreach (string test in districtname)
            {
                if (test != "Not Available")
                {
                    ListItem listItem = new ListItem();
                    listItem.Attributes.Add("runat", "server");
                    listItem.Value = test;
                    listItem.Text = test;
                    chklist1.Items.Add(listItem);
                    //pnlDistrictList.Controls.Add(chklist1);
                }
            }
        }

        public void MakeAreaCheckBox(List<string> areaname)
        {
            int count = areaname.Count;

            foreach (string test in areaname)
            {
                if (test != "Not Available")
                {
                    ListItem listItem = new ListItem();
                    listItem.Attributes.Add("runat", "server");
                    listItem.Value = test;
                    listItem.Text = test;
                    lstAreaList.Items.Add(listItem);
                    //pnlDistrictList.Controls.Add(chklist1);
                }
            }
        }

        /// <summary>
        /// This function is used for Image map control
        /// This function redirects the user to appropriate Page with the same mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imageMap_Click(object sender, ImageMapEventArgs e)
        {
            string mode = Convert.ToString(Session["statusPage"]);

            #region Image Map Redirect for Jammu Kashmir
            if (e.PostBackValue.Contains("Jammu Kashmir"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jammu Kashmir&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jammu Kashmir&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jammu Kashmir&Mode=share");
                }
            }
            #endregion for JammuKashmir

            #region Image Map Redirect for Bihar
            else if (e.PostBackValue.Contains("Bihar"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Bihar&Mode=rent");
                }
                else if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Bihar&Mode=buy");
                }
                else if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Bihar&Mode=share");
                }
            }
            #endregion Image Map Redirect for Bihar

            #region Image Map Redirect for Jharkhand
            else if (e.PostBackValue.Contains("Jharkhand"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jharkhand&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jharkhand&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Jharkhand&Mode=share");
                }
            }
            #endregion Image Map Redirect for Jharkhand

            #region Image Map Redirect for Himachal Pradesh
            if (e.PostBackValue.Contains("Himachal Pradesh"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Himachal Pradesh&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Himachal Pradesh&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Himachal Pradesh&Mode=share");
                }
            }
            #endregion for Himachal Pradesh

            #region Image Map Redirect for Uttar Pradesh
            if (e.PostBackValue.Contains("Uttar Pradesh"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Uttar Pradesh&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Uttar Pradesh&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Uttar Pradesh&Mode=share");
                }
            }
            #endregion for Uttar Pradesh

            #region Image Map Redirect for Delhi
            if (e.PostBackValue.Contains("Delhi"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Delhi&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Delhi&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Delhi&Mode=share");
                }
            }
            #endregion for Delhi

            #region Image Map Redirect for Haryana
            if (e.PostBackValue.Contains("Haryana"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Haryana&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Haryana&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Haryana&Mode=share");
                }
            }
            #endregion for Haryana

            #region Image Map Redirect for Punjab
            if (e.PostBackValue.Contains("Punjab"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Punjab&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Punjab&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Punjab&Mode=share");
                }
            }
            #endregion for Punjab

            #region Image Map Redirect for Chandigarh
            if (e.PostBackValue.Contains("Chandigarh"))
            {
                if (mode.Equals("rent"))
                {
                    Response.Redirect("~\\Property.aspx?State=Chandigarh&Mode=rent");
                }
                if (mode.Equals("buy"))
                {
                    Response.Redirect("~\\Property.aspx?State=Chandigarh&Mode=buy");
                }
                if (mode.Equals("share"))
                {
                    Response.Redirect("~\\Property.aspx?State=Chandigarh&Mode=share");
                }
            }
            #endregion for Chandigarh
        }

        /// <summary>
        /// This function is used for Getting the area name
        /// on the basis of what district has been selected
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGetArea_Click(object sender, EventArgs e)
        {
            string selectedState = Request["State"];
            string currentMode = Request["Mode"];
            string selected = "";
            List<String> selecteddistricts = new List<string>();
            for (int i = 0; i <= chklist1.Items.Count - 1; i++)
            {
                if ((chklist1.Items[i].Selected) == true)
                {
                    string s = chklist1.Items[i].Value;

                    selecteddistricts.Add(s);
                    //TextBox1.Text += s;
                    selected += "&district=" + s;
                }
            }
            Response.Redirect("~//Property.aspx?State=" + selectedState + "&Mode=" + currentMode + selected);
        }


        /// <summary>
        /// This function is used for Searching the property
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
           String area = "";
            
            if (BuySearch.Suburb.ToString() != "")
            {
                Response.Redirect("public/searchProperty.aspx?&Mode=" 
                    + Request["Mode"] 
                    + "&area=" + BuySearch.Suburb.ToString() 
                    + "&minPrice=" + BuySearch.minprice.ToString()
                    + "&maxPrice=" + BuySearch.maxprice.ToString()
                    + "&numBedRoom=" + BuySearch.bedrooms.ToString());
            }
            else if (txtSuburb.Text.ToString() != "")
            {
                Response.Redirect("public/searchProperty.aspx?&Mode="
                    + Request["Mode"]
                    + "&area=" + txtSuburb.Text.ToString()
                    + "&minPrice=" + txtMinPrice.Text.ToString()
                    + "&maxPrice=" + txtMaxPrice.Text.ToString()
                    + "&numBedRoom=" + txtBedRoom.Text.ToString());
            }
            else
            {
                for (int i = 0; i <= lstAreaList.Items.Count - 1; i++)
                {
                    if ((lstAreaList.Items[i].Selected) == true)
                    {
                        area += "&area=" +lstAreaList.Items[i].Value;
                    }
                }
                Response.Redirect("public/searchProperty.aspx?&Mode="
                    + Request["Mode"]
                    + area
                    + "&minPrice=" + ""
                    + "&maxPrice=" + ""
                    + "&numBedRoom=" + "");
            }
        }
    }
}
